/**
 * author      : dormi330
 * date        : 2018/6/28
 * project     : mybatis
 * description : 订单controller
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.application.CartServiceFacade;
import com.deep.trade.cart.InDtoCart;
import com.deep.trade.common.HttpResonsePayload;
import com.deep.trade.infrastructure.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;


@Controller
@RequestMapping(value = "/cart")
public class CartController {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private CartServiceFacade cartServiceFacade;

    /**
     * 添加sku到购物车
     *
     * @param inDtoCart 输入参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResonsePayload addSkuToCart(@RequestBody InDtoCart inDtoCart) {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCart);

        try {
            cartServiceFacade.addSkuToCart(inDtoCart);
            return HttpResonsePayload.happy(true);
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResonsePayload.unhappy("1", ex.getMessage());

        }
    }

}
